package pl.edu.wsinf.lukaszwalczak.emailclient.service;

import java.util.List;
import javax.inject.Inject;
import org.springframework.stereotype.Component;
import pl.edu.wsinf.lukaszwalczak.emailclient.pojo.Account;
import pl.edu.wsinf.lukaszwalczak.emailclient.pojo.Folder;
import pl.edu.wsinf.lukaszwalczak.emailclient.repository.AccountRepository;
import pl.edu.wsinf.lukaszwalczak.emailclient.repository.FolderRepository;
import pl.edu.wsinf.lukaszwalczak.emailclient.repository.MailRepository;

@Component
public class AccountService {

    private static final String SENT_FOLDER_NAME = "Wysłane";
    private static final String TRASH_FOLDER_NAME = "Kosz";
    private static final String DRAFTS_FOLDER_NAME = "Wersje robocze";
    @Inject
    private FolderRepository folderRepository;
    @Inject
    private AccountRepository accountRepository;
    @Inject
    private MailRepository mailRepository;

    public Account getFirstAccount() {
        List<Account> list = accountRepository.findAll();
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public Folder getSentFolder(Account account) {
        Folder sentFolder = folderRepository.findByAccountAndName(account, SENT_FOLDER_NAME);
        if (sentFolder == null) {
            sentFolder = folderRepository.save(new Folder(account, SENT_FOLDER_NAME));
        }
        return sentFolder;
    }

    public Folder getTrashFolder(Account account) {
        Folder trashFolder = folderRepository.findByAccountAndName(account, TRASH_FOLDER_NAME);
        if (trashFolder == null) {
            trashFolder = folderRepository.save(new Folder(account, TRASH_FOLDER_NAME));
        }
        return trashFolder;
    }

    public Folder getDraftsFolder(Account account) {
        Folder draftsFolder = folderRepository.findByAccountAndName(account, DRAFTS_FOLDER_NAME);
        if (draftsFolder == null) {
            draftsFolder = folderRepository.save(new Folder(account, DRAFTS_FOLDER_NAME));
        }
        return draftsFolder;
    }
}
